package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.config.AliPay;
import cn.itsource.ymcc.service.IPayOrderService;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.query.PayOrderQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.util.AssertUtil;
import cn.itsource.ymcc.vo.PayVo;
import com.alipay.easysdk.factory.Factory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/payOrder")
public class PayOrderController {

    @Autowired
    public IPayOrderService payOrderService;

    /**
     * 我们的项目在本地
     *
     * 支付宝的沙箱环境在线上   线上通知线下  肯定不行
     * 解决办法把 自己搞线上的  内网穿透来解决
     * @param request
     * @return
     */
    @PostMapping("/asyncNotify")
    public String asyncNotify(HttpServletRequest request) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,String> parameter = new HashMap<>();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            parameter.put(entry.getKey(), entry.getValue()[0]);
        }
        //进行二次验签  和支付宝做判断   要保证  我得到的数据和支付宝的数据要一致
        Boolean aBoolean = Factory.Payment.Common().verifyNotify(parameter);
        if(aBoolean && "TRADE_SUCCESS".equals(parameter.get("trade_status"))){
            payOrderService.resultProcessing(parameter);
            return  "success";
        }
        return "failure";
    }

    @PostMapping("/apply")
    public JsonResult apply(@RequestBody @Valid PayVo payVo){
        return payOrderService.apply(payVo);
    }

    @GetMapping("/checkPayOrder/{orderNo}")
    public JsonResult checkPayOrder(@PathVariable("orderNo")String orderNo){
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));
        AssertUtil.isNotNull(one,"未查询到该支付单");
        return JsonResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody PayOrder payOrder){
        if(payOrder.getId()!=null){
            payOrderService.updateById(payOrder);
        }else{
            payOrderService.save(payOrder);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        payOrderService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(payOrderService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(payOrderService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody PayOrderQuery query){
        Page<PayOrder> page = new Page<PayOrder>(query.getPageNo(), query.getPageSize());
        page = payOrderService.page(page);
        return JsonResult.success().setData(new PageList<PayOrder>(page.getTotal(), page.getRecords()));
    }
}
